from test_emotion import Sentiment
import argparse

from flask import Flask
from flask import request
from flask import make_response
from flask_restful import reqparse, Api, Resource
from gevent.pywsgi import WSGIServer
from flask_cors import CORS
class SentimentServer(Resource):
    def __init__(self,sentiment):
        self.sentiment=sentiment
    def post(self):
        args = parser.parse_args()

        inputstr = args.get("input")
        print(inputstr)
        if len(inputstr) == 0:
            return {"sentiment": [], "success": False}, 200
        # 获取情感
        st=sentiment.get_sentiment(inputstr)
        return {"sentiment": st, "success": True}, 200
if __name__ == '__main__':

    sentiment=Sentiment()
    # server
    app = Flask(__name__)
    CORS(app, supports_credentials=True)
    api = Api(app)
    parser = reqparse.RequestParser()
    parser.add_argument("input", type=str)
    api.add_resource(SentimentServer, "/sentiment", resource_class_kwargs={"sentiment": sentiment
            })

    WSGIServer(("0.0.0.0", 10016), app).serve_forever()